package com.token.sysorg.services;

import com.token.sql.IntegrateSql;

import java.sql.ResultSet;

import com.token.base.FuncUtil;
import com.token.base.Pl;
import com.token.sysorg.bean.FSysDepartment;
/**
 * 写组织架构_部门
 * @author 代码创建
 */
public class FSysDepartmentWrite{
	private String datetime = Pl.getCurDataTime();
	private String user;
	private int sysid = Pl.getSysId();
	public  void writeData(FuncUtil F,IntegrateSql iSql) throws Exception {
		String keyvalue = F.getUrlQueryString("DeptID");
		FSysDepartment dao = new FSysDepartment(iSql);
		dao.setInsertOrUpdateRs(keyvalue);
		user = F.getEffectiveUserName();
		if(dao.isUpdate()){
			dao.setSysEditTime(datetime); //修改时间
			dao.setSysEditUser(user); //修改人
			dao.setDepartmentNumber(F.getUrlQueryString("DepartmentNumber")); //公司ID
		}else{
			dao.setSysId(sysid); //租户ID //json.getIntValue("SysId")
			dao.setSysAddTime(datetime); //创建时间
			dao.setSysAddUser(user); //创建人
			dao.setSysEditTime(datetime); //修改时间
			dao.setSysEditUser(user); //修改人
			dao.setDepartmentNumber(getComNum(dao.getTableName(), iSql)); //公司ID
		}
		dao.setParentDeptID(F.getUrlQueryString("ParentDeptID")); //上级ID
//		dao.setDepartmentNumber(F.getUrlQueryString("DepartmentNumber")); //部门编号
		dao.setDepartmentName(F.getUrlQueryString("DepartmentName")); //部门名称
		dao.setComID(F.getUrlQueryString("ComID")); //所属公司编号
		dao.setLeaderID(F.getUrlQueryString("LeaderID")); //领导ID
		dao.setLeaderName(F.getUrlQueryString("LeaderName")); //领导名称
		dao.setManagerID(F.getUrlQueryString("ManagerID")); //管理者ID
		dao.setManagerName(F.getUrlQueryString("ManagerName")); //管理者名称
		dao.setInChargeLeaderID(F.getUrlQueryString("InChargeLeaderID")); //分管领导ID
		dao.setInChargeLeaderName(F.getUrlQueryString("InChargeLeaderName")); //分管领导名称
		dao.executeUpdate();
		dao.close();

	}

	private String getComNum(String tablename, IntegrateSql iSql) throws Exception{
		String sql = "SELECT Max(DepartmentNumber) AS UnitID  FROM " + tablename;
		ResultSet rs = iSql.executeQuery(sql);
		if(rs.next()){
			String num = rs.getString("UnitID");
			int id = Integer.parseInt(num.substring(1, num.length()));

			return buling("D", (id+1)+"");
		}else{
			return "D001";
		}
	}

	private String buling(String str1,String str2) {
		for(int i=str2.length();i<3;i++){
			str2 = "0" + str2;
		}
		return str1 + str2;
	}
}
